﻿using NFine.Domain._03_Entity.pay;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace NFine.Application.User
{
    public class xmlToolApp
    {
        public string GetTextBydoc(string text,string tag)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(text);
            string restr="";
            try
            {
                restr= doc.GetElementsByTagName(tag)[0].InnerText;
                return restr;
            }
            catch (Exception)
            {

                return restr;
            }
       
        }
        public string GetTextByurl(string url, string tag)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(url);
            return doc.GetElementsByTagName(tag)[0].InnerText;
        }
        public List<JHPayList> XmlToList(string xml)
        {
            XmlDocument doc = new XmlDocument();
            try
            {
                doc.LoadXml(xml);
            }
            catch
            {
                return null;
            }
            //if (doc.ChildNodes.Count != 1)
            //    return null;
            XmlNodeList node = doc.GetElementsByTagName("LIST");

            List<JHPayList> items = new List<JHPayList>();

            foreach (XmlNode child in node)
            {              
                    items.Add(XmlNodeToEntity(child));
            }

            return items;
        }
        public  JHPayList XmlNodeToEntity(XmlNode node)
        {
            JHPayList item = new JHPayList();

            if (node.NodeType == XmlNodeType.Element)
            {
                XmlElement element = (XmlElement)node;

                System.Reflection.PropertyInfo[] propertyInfo =
            typeof(JHPayList).GetProperties(System.Reflection.BindingFlags.Public |
            System.Reflection.BindingFlags.Instance);

                foreach (XmlNode attr in element.ChildNodes)
                {
                    string attrName = attr.Name;
                    string attrValue = attr.InnerText.ToString();
                    foreach (System.Reflection.PropertyInfo pinfo in propertyInfo)
                    {
                        if (pinfo != null)
                        {
                            string name = pinfo.Name;
                            Type dbType = pinfo.PropertyType;
                            if (name == attrName)
                            {
                                if (String.IsNullOrEmpty(attrValue))
                                    continue;
                                switch (dbType.ToString())
                                {
                                    case "System.Int32":
                                        pinfo.SetValue(item, Convert.ToInt32(attrValue), null);
                                        break;
                                    case "System.Boolean":
                                        pinfo.SetValue(item, Convert.ToBoolean(attrValue), null);
                                        break;
                                    case "System.DateTime":
                                        pinfo.SetValue(item, Convert.ToDateTime(attrValue), null);
                                        break;
                                    case "System.Decimal":
                                        pinfo.SetValue(item, Convert.ToDecimal(attrValue), null);
                                        break;
                                    case "System.Double":
                                        pinfo.SetValue(item, Convert.ToDouble(attrValue), null);
                                        break;
                                    default:
                                        pinfo.SetValue(item, attrValue, null);
                                        break;
                                }
                                continue;
                            }
                        }
                    }
                }
            }
            return item;
        }
        public List<T> XmlToList<T>(string xml) where T : new()
        {
            XmlDocument doc = new XmlDocument();
            try
            {
                doc.LoadXml(xml);
            }
            catch
            {
                return null;
            }
            //if (doc.ChildNodes.Count != 1)
            //    return null;
            XmlNodeList node = doc.GetElementsByTagName("LIST");

            List<T> items = new List<T>();

            foreach (XmlNode child in node)
            {
                items.Add(XmlNodeToEntity<T>(child));
            }

            return items;
        }
        public T XmlNodeToEntity<T>(XmlNode node) where T : new()
        {
            T item = new T();

            if (node.NodeType == XmlNodeType.Element)
            {
                XmlElement element = (XmlElement)node;

                System.Reflection.PropertyInfo[] propertyInfo =
            typeof(T).GetProperties(System.Reflection.BindingFlags.Public |
            System.Reflection.BindingFlags.Instance);

                foreach (XmlNode attr in element.ChildNodes)
                {
                    string attrName = attr.Name;
                    string attrValue = attr.InnerText.ToString();
                    foreach (System.Reflection.PropertyInfo pinfo in propertyInfo)
                    {
                        if (pinfo != null)
                        {
                            string name = pinfo.Name;
                            Type dbType = pinfo.PropertyType;
                            if (name == attrName)
                            {
                                if (String.IsNullOrEmpty(attrValue))
                                    continue;
                                switch (dbType.ToString())
                                {
                                    case "System.Int32":
                                        pinfo.SetValue(item, Convert.ToInt32(attrValue), null);
                                        break;
                                    case "System.Boolean":
                                        pinfo.SetValue(item, Convert.ToBoolean(attrValue), null);
                                        break;
                                    case "System.DateTime":
                                        pinfo.SetValue(item, Convert.ToDateTime(attrValue), null);
                                        break;
                                    case "System.Decimal":
                                        pinfo.SetValue(item, Convert.ToDecimal(attrValue), null);
                                        break;
                                    case "System.Double":
                                        pinfo.SetValue(item, Convert.ToDouble(attrValue), null);
                                        break;
                                    default:
                                        pinfo.SetValue(item, attrValue, null);
                                        break;
                                }
                                continue;
                            }
                        }
                    }
                }
            }
            return item;
        }
    }
}
